// pages/order/order.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    noticeTxt: "",
    pageNumber: 1,
    pageSize: 999999,
    totalPage: 1,
    showForm: true,
    showDateDio: false,
    startDate: "",
    endDate: "",
    search: {
      task: "",
      workerStr: "",
      isDeleted: false,
      creator: "",
      planDoneDate: [],
      onlineDate: [],
      workType: [],
      type: [],
      status: [],
      todayDone: false,
      startLevel: [],
    },
    taskList: [],
    project: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      project: JSON.parse(options.project),
      noticeTxt: app.getNoticeTxt(),
      startDate: app.formatDate(),
      endDate: app.formatDate(),
    });
  },
  showWorkerOptions(){
    this.setData({
      showWorkers: true
    });
  },

  closeSearch(){
    this.setData({
      showDateDio: false,
    });
    this.getTasks();
  },

  bindDateChange(e){
    if(e.currentTarget.dataset.type == "startDate"){
      this.setData({
        startDate: e.detail.value.replace(/-/g, "/")
      });
    }else if(e.currentTarget.dataset.type == "endDate"){
      this.setData({
        endDate: e.detail.value.replace(/-/g, "/")
      });
    }
  },

  onDisplayShowDate(){
    this.setData({
      showDateDio: true
    });
  },

  onCloseDate(e){
    console.log("onCloseDate", e.detail)
  },

  onConfirmDate(e){
    console.log("onConfirmDate", e.detail)
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getTasks(){
    let obj = {
      // creator: this.search.creator,
      endDate: this.data.endDate,
      // isDeleted: this.search.isDeleted ? "Y" : "N",
      pageNumber: this.data.pageNumber,
      pageSize: this.data.pageSize,
      projectId: this.data.project.id,
      seniorFlag: "Y",
      startDate: this.data.startDate,
      // startLevel: this.search.startLevel.toString(),
      // status: this.search.status.toString(),
      // task: this.search.task,
      // type: this.search.type.toString(),
      versionId: "",
      // versionend: (this.search.onlineDate && this.search.onlineDate[1]) || "",
      // versionstart: (this.search.onlineDate && this.search.onlineDate[0])|| "",
      // workType: this.search.workType.toString(),
      workerStr: wx.getStorageSync('user').name,
    }
    console.log(obj);
    app.AjaxPost({
      p0: {
        appName: "s6-service",
        methodName: "getTaskList",
        params: obj,
        serviceName: "taskService"
      },
      servicename: "customService"
    }, (res)=>{
      console.log("tasks返回：", res)
      if(res.status === "success"){
        let _this = this;
        res.data.taskList.forEach((element)=>{
          element.expFinishDateFormat = app.formatDate(element.expFinishDate);
          element.levelValue = app.getDictionaryLabel("startLevelOptions", element.level);
          element.statusValue = app.getDictionaryLabel("statusOptions", element.status);
          element.taskTypeValue = app.getDictionaryLabel("taskTypeOptions", element.taskType);
          element.typeValue = app.getDictionaryLabel("typeOptions", element.type);
          element.taskId = element.id;
          element.zeeWaitRelease = false;
          element.zeeWaitComplete = false;
          element.zeeWaitAcceptance = false;
          element.zeeWaitAdopt = false;
          element.zeeWaitCancel = false;
          // 是否需要今天完成
          element.needTodayComplate = app.formatDate(element.expFinishDate) === app.formatDate(new Date().getTime());
          let qx = element.worker === wx.getStorageSync('user').name || "张杨鹏" === wx.getStorageSync('user').name;
          // 根据状态判断按钮是否可见
          if(element.status == "W"){
            // 工作中
            element.admin == true ? element.zeeWaitComplete = true : element.zeeWaitComplete = qx;
          }else if(element.status == "WS"){
            // 等待
            element.admin == true ? element.zeeWaitRelease = true : element.zeeWaitRelease = qx;
          }else if(element.status == "WA"){
            // 待验收
            element.admin == true ? element.zeeWaitAcceptance = true : element.zeeWaitAcceptance = qx;
          }else if(element.status == "A"){
            // 验收通过
            element.admin == true ? element.zeeWaitAdopt = true : element.zeeWaitAdopt = qx;
          }else if(element.status == "C"){
            // 取消
            element.admin == true ? element.zeeWaitCancel = true : element.zeeWaitCancel = qx;
          }
        });
        console.log("tasks返回：", res.data.taskList)
        _this.setData({
          taskList: res.data.taskList || [],
          totalPage: Math.ceil(res.data.listSizes / _this.data.pageSize)
        });
      }
    });
  },

  userAction(e){
    console.log(e.currentTarget);
    let ids = e.currentTarget.dataset.task.taskId + ",";
    app.AjaxPost({
      p0: {
        appName: "s6-service",
        methodName: "taskScheduling",
        params: {
          action: e.currentTarget.dataset.type,
          taskId: ids
        },
        serviceName: "taskService"
      },
      servicename: "customService"
    }, (res)=>{
      console.log("用户点击返回：", res)
      if(res.status === "success"){
        let _this = this;
        this.getTasks();
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    if(this.data.message == "scanback"){
      // 返回页-返回
      console.log(this.data)
    }else{
      // 主页-跳转
      this.getTasks();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  refreshTasks(){
    this.getTasks();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNumber: 1
    });
    this.getTasks();
    wx.showNavigationBarLoading(); //在标题栏中显示加载图标
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // let num = this.data.pageNumber;
    // if(num > this.data.totalPage){
    //   console.log("上拉加载完毕");
    //   return 
    // }else{
    //   num ++;
    //   this.setData({
    //     pageNumber: num
    //   });
    //   this.getTasks();
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})